package com.example.firstdemo

import androidx.compose.material3.Scaffold
import androidx.compose.material3.SnackbarDuration
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.getValue
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.staticCompositionLocalOf
import androidx.navigation.NavDestination.Companion.hasRoute
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.compose.rememberNavController
import com.example.firstdemo.network.service.AuthManager
import com.example.firstdemo.network.service.NetworkErrorEventManager
import com.example.firstdemo.ui.layout.AuthEventDialog
import com.example.firstdemo.ui.layout.BottomNavigation
import com.example.firstdemo.ui.screens.components.SnackbarType
import com.example.firstdemo.ui.screens.components.TinyMessageBox
import com.example.firstdemo.ui.screens.components.TinyMessageBoxSate
import com.example.firstdemo.ui.screens.components.ZoomableImage
import com.example.firstdemo.ui.screens.components.ZoomableImageState
import com.example.firstdemo.ui.screens.pages.login.navigateToLogin
import com.example.firstdemo.ui.theme.AppTheme
import com.example.firstdemo.utils.enterTransition
import com.example.firstdemo.utils.exitTransition
import com.example.firstdemo.utils.popEnterTransition
import com.example.firstdemo.utils.popExitTransition
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

@Composable
fun App() {
    AppTheme {
        MainScreen()
    }
}

val LocalTinyMessageBoxState = staticCompositionLocalOf {
    TinyMessageBoxSate()
}

val LocalZoomableImageState = staticCompositionLocalOf {
    ZoomableImageState()
}

fun networkMonitor(tinyMessageBoxSate: TinyMessageBoxSate) {
    CoroutineScope(Dispatchers.Main).launch {
        NetworkErrorEventManager.networkErrorEvent.collect { msg ->
            tinyMessageBoxSate
                .showSnackbar(
                    type = SnackbarType.ERROR,
                    message = msg,
                    actionLabel = null,
                    withDismissAction = true,
                    duration = SnackbarDuration.Short
                )
        }
    }
}

@Composable
fun MainScreen() {
    val navController = rememberNavController()
    val currentBackStackEntry by navController.currentBackStackEntryAsState()
    val currentDestination = currentBackStackEntry?.destination
    val showBottomBar = destinationWithBottomBarList.any {
        currentDestination?.hasRoute(it::class) == true
    }


    CompositionLocalProvider(
        LocalTinyMessageBoxState provides TinyMessageBoxSate(),
        LocalZoomableImageState provides ZoomableImageState()
    ) {
        Scaffold(
            bottomBar = {
                if (showBottomBar) {
                    BottomNavigation(navController)
                }
            }
        ) { innerPadding ->
            NavHost(
                navController = navController,
                enterTransition = { enterTransition() },
                exitTransition = { exitTransition() },
                popEnterTransition = { popEnterTransition() },
                popExitTransition = { popExitTransition() },
                graph = getNavGraph(navController, innerPadding)
            )
        }

        // 认证失败确认弹框
        val coroutineScope = rememberCoroutineScope()
        AuthEventDialog {
            coroutineScope.launch {
                AuthManager.logout()
            }
            navController.navigateToLogin {
                popUpTo(0)
            }
        }

        TinyMessageBox(LocalTinyMessageBoxState.current)
        networkMonitor(LocalTinyMessageBoxState.current)
        ZoomableImage(LocalZoomableImageState.current)
    }
}